package com.sgb.member.view_model;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.MemberDoorListBean;
import com.sgb.member.entity.Record;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MemberDoorModel extends ViewModel {

    public MutableLiveData<List<CommonPopEntity>> tab1LiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> tab2LiveData = new MutableLiveData<>();

    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> isLoadLiveData = new MutableLiveData<>();
    public MutableLiveData<List<Record>> listLiveData = new MutableLiveData<>();

    public void getTab1Data() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "不限"));
        list.add(new CommonPopEntity("1", "供方会员"));
        list.add(new CommonPopEntity("2", "需方会员"));
        tab1LiveData.postValue(list);
    }

    public void getTab2Data(String id) {
        isLoadLiveData.postValue(true);
        // 会员类型 0 供方 1需方
        if(!TextUtils.isEmpty(id)){
            id = TextUtils.equals("1", id) ? "0" : "1";
        }
        MemberConfig.getInstance().getApi().getMemberTypeList(id).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                isLoadLiveData.postValue(false);
                if (data != null && data.getData() != null) {
                    List<CommonPopEntity> list = new ArrayList<>();
                    list.add(new CommonPopEntity("", "不限"));
                    list.addAll(data.getData());
                    tab2LiveData.postValue(list);
                } else {
                    msgLiveData.postValue(data.getMsg());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
                isLoadLiveData.postValue(false);
            }
        });
    }


    public void getListData(String firstCategory,
                            String secondCategory,
                            String thirdCategory,
                            String relation, String keyWord, int page) {

        Map<String, Object> map = new HashMap<>();
        map.put("firstCategory", firstCategory);
        map.put("secondCategory", secondCategory);
        map.put("thirdCategory", thirdCategory);
        map.put("relation", relation);//供需关系1:供方2:需方
        map.put("keyWord", keyWord);
        map.put("page", page);
        map.put("limit", 10);
        isLoadLiveData.postValue(true);

        MemberConfig.getInstance().getApi().getMemberDoorList(map).enqueue(new ICallBack<BaseEntity<MemberDoorListBean>>() {
            @Override
            public void onSuccess(BaseEntity<MemberDoorListBean> data) {
                isLoadLiveData.postValue(false);
                if (data != null && data.getData() != null) {
                    listLiveData.postValue(data.getData().getRecords());
                } else {
                    msgLiveData.postValue(data.getMsg());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
                isLoadLiveData.postValue(false);
            }
        });
    }

}
